package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHUtil
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

/**
 *
 * @use 保存更名工单合同信息[智能工单回写]
 *
 */
public class GetSaveRenameWordOrderStorecontract implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(GetSaveRenameWordOrderStorecontract.class);
	private String charset = "utf-8"; //设置字符集
	private int timeout = 100000; //设置请求超时时间
	private int enableSSLflag = 1;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		JSONObject result = new JSONObject();
		def smap,oJson,SaveResult;
		try {
			//获取客户端传参
			def r_params = Util.filterForObject(request.getParameter("params")); //获取json字符串
			log.debug("NGS传参:"+r_params+" | ");
			//传参处理

			//列表接口调用
			if(r_params.equals("")) {
				result.put("code", "-1");
				result.put("msg", "未传递参数！");
				return result
			}
			Map<String, String> map = new HashMap<String, String>();
			map.put("params", r_params);
			map.put("key", "ca3c64a49d1f11ecbed6d703198897725e7aba55");
			def	ASCIIMD5 = KHUtil.ASCIIORDER(map);
			log.debug("接口返回:"+ASCIIMD5);
			oJson = CustHttpPort(eliteBean,ASCIIMD5); //调用接口方法
			log.debug("接口返回:"+oJson);
			return	oJson;

		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "调用失败！");
			return result;
		}
	}

/**
 *
 @use 保存更名工单合同信息[智能工单回写]
 *
 */
	private String CustHttpPort(EliteBean eliteBean,ASCIIMD5) {
		JSONObject result = new JSONObject();
		def smap,mopsmap,chatqueue,customer_guid,url="";
		try {
			//获取接口地址
			smap = eliteBean.chaxun_sql("select target_url as url from sys_interface_config where type ='saveRenameWordOrderStorecontract'","dataSource");
			if(smap.size()==0) {
				result.put("msg", "找不到对应接口");
				result.put("code", "-1");
				return result;
			}else{
				url = smap[0].getAt("url");
			};

			def postmap=Util.doHttpPost(url,ASCIIMD5.toString(),charset,timeout,enableSSLflag);
			log.debug("接口返回:"+postmap);
			JSONObject obj = JSONObject.fromObject(postmap);
			if(obj.getInt("status")!=200) {
				result.put("code","-1");
				result.put("msg", obj.getString("message"));
				return result;
			}else{
				return obj;
			};

		}catch (Exception e) {
			log.error("errormsg:", e)
			result.put("code", "-1");
			result.put("msg", "程序异常");
			return result;
		}
	}

	public String ASCIIORDER(Map<String, String> map) {
		JSONObject result = new JSONObject();
		// 对所有传入参数按照字段名的 ASCII 码从小到大排序
		List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
		Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
			public int compare(Map.Entry<String, String> A, Map.Entry<String, String> B) {
				return (A.getKey()).toString().compareTo(B.getKey());
			}
		});

		// 构造签名键值的格式
		StringBuilder stringsign = new StringBuilder();
		StringBuilder sign = new StringBuilder();
		for (Map.Entry<String, String> item : infoIds) {
			if (item.getKey() != null || item.getKey() != "") {
				String key = item.getKey();
				String val = item.getValue();
				if (!(val == "" || val == null)) {
					sign.append(key + "=" + URLEncoder.encode(val, "UTF-8") + "&");
				}
				if (!(val == "" || val == null)&&!(key == "key")) {
					stringsign.append(key + "=" + val + "&");
				}
			}
		}
		String md5sign = Util.getMD5Str(sign.substring(0, sign.length()-1));
		return stringsign.append("sign="+md5sign);
	}
}
